/*
 * @(#)HttpClientTemplate.java $version 2011. 11. 25.
 *
 */

package com.mywep.myalimi.common.http;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

/**
 * @author 한지웅
 */
public class HttpClientTemplate {
	private static final Logger LOG = LoggerFactory.getLogger(HttpClientTemplate.class);

	/**
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public String execute(String url) throws Exception {
		HttpComponentsClientHttpRequestFactory clientRequest = new HttpComponentsClientHttpRequestFactory();
		HttpGet httpGet = new HttpGet(url);
		HttpClient httpClient = clientRequest.getHttpClient();
		HttpResponse httpResponse = httpClient.execute(httpGet);
		HttpEntity entity = httpResponse.getEntity();

		BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
		StringBuilder contents = new StringBuilder();
		String str;
		while ((str = br.readLine()) != null) {
			contents.append(str);
		}

		httpClient.getConnectionManager().shutdown();

		return contents.toString();
	}


}
